<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/10/25
  Time: 9:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="script/jquery-3.6.0.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#getJson").click(function (){
                // console.log("ok~")
                var url=this.href
                var args={"time":new Date}//这是要发送的参数，防止页面缓存
                $.post(
                    url,
                    args,
                    function (data) {
                        console.log("data=",data)
                        console.log("data.name=",data.name)
                        console.log("data.address=",data.address)
                    },
                    "json"
                )
                return false;//返回false,就不适用href默认机制
            })
            $("button[name='butt1']").click(function () {
                var url="/springmvc/save2";
                var userName = $("#userName").val();
                var age = $("#age").val();
                //json格式的数据转成json格式的字符串
                var args =JSON.stringify({"userName":userName,"age":age})
                $.ajax({
                    url:url,
                    data:args,
                    type:"post",
                    success:function (data) {
                        console.log("data=",data)
                        // console.log("data.name=",data.name)
                        // console.log("data.age=",data.age)

                    },
                    contentType:"application/json;charset=utf-8"
                })

            })

        })
    </script>
</head>
<body>
<h1>请求一个json数据</h1>
<a href="<%=request.getContextPath()%>/json/dog" id="getJson">点击获取json数据</a>

<h1>发布一个jsons数据</h1>
u:<input id="userName" type="text"><br/>
a:<input id="age" type="text"><br/>
<button name="butt1">添加用户</button>
<h1>下载图片</h1>
<a href="<%=request.getContextPath()%>/downFile">点击下载文件</a>
</body>
</html>
